#include <iostream>
#include <memory>
#include "Connection.hpp"
#include "Reactor.hpp"
#include "HandlerConnection.hpp"
#include "Service.hpp"
#include "Listener.hpp"
#include "Cal.hpp"

void Useage(const std::string& str)
{
    std::cout << "Useage:\n\t" << str << " port \n";
    exit(USE_ERR);
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        Useage(argv[0]);
    }

    uint16_t port = std::stoi(argv[1]);

    Service service(Cal);
    HandlerConnection hc(std::bind(&Service::Excute, &service, std::placeholders::_1));
    Listener listen(port, hc);

    std::unique_ptr<Reactor> usvr = std::make_unique<Reactor>();
    usvr->AddConnection(listen.Socket(), EPOLLIN | EPOLLET, 
        std::bind(&Listener::AcceptClient, &listen, std::placeholders::_1), nullptr, nullptr);

    usvr->Dispatcher();

    return 0;
}